package com.xoa.controller.workApp;


import com.xoa.dao.WFE.WFEFlowRunMapper;
import com.xoa.dao.WFE.WFEFlowRunPrcsMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.workApp.LimsProject;
import com.xoa.dao.workApp.LimsProjectMapper;
import com.xoa.dao.workflow.RelationFuncMapper;
import com.xoa.model.department.Department;
import com.xoa.model.users.Users;

import com.xoa.model.workApp.LimsProjectExample;
import com.xoa.model.workflow.FlowRun;
import com.xoa.model.workflow.RelationFunc;
import com.xoa.model.workflow.RelationFuncExample;
import com.xoa.service.WFE.WFERunner.WFEFlowRun;
import com.xoa.service.WFE.WFERunner.WFEFlowRunPrcs;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.LimsJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/workapp/work")

public class WorkAppController {
    @Autowired
    private LimsProjectMapper limsProjectMapper;

    @Autowired
    private RelationFuncMapper relationFuncMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    protected WFEFlowRun wfeFlowRun;

    @Autowired
    WFEFlowRunMapper wfeFlowRunMapper;

    @Autowired
    WFEFlowRunPrcsMapper wfeFlowRunPrcsMapper;

    @Autowired
    protected WFEFlowRunPrcs wfeFlowRunPrcs;
    //禁止跳转
    @RequestMapping("/workApp403")
    public String workApp403(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "/app/workapp/work/workApp403";
    }

    //流程转交下一步PAD页面
    @RequestMapping("/schedulePad")
    public String schedulePad(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "/app/lims/apad/schedulePad";
    }

    @RequestMapping("workformApp")
    public String workformApp(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "app/workapp/work/workformApp";
    }

    @RequestMapping("inputReview")
    public String inputReview(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "app/workapp/work/inputReview";
    }

    @RequestMapping("index")
    public String index(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "app/workapp/work/index";
    }

    @RequestMapping("projQuotation")
    public String projQuotation(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "app/workapp/work/projQuotation";
    }

    @RequestMapping("operatingConditions")
    public String operatingConditions(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "app/workapp/work/operatingConditions";
    }

    @RequestMapping("entrustmentSlip")
    public String entrustmentSlip(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "app/workapp/work/entrustmentSlip";
    }

    @RequestMapping("viewproject")
    public String viewproject(HttpServletRequest request) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse"));
        return "app/workapp/work/viewproject";
    }

    /**
    * 创建作者:   刘 欢
    * 创建日期:   16:47 2020/4/9
    * 方法介绍:   判断新建的时候是不是又runid，runid下面有没有对应的projectId
    * 参数说明:   * @param runId
    * 返回值类型：com.xoa.util.lims.LimsJson
    */

    @RequestMapping("selectProjectByRunId")
    @ResponseBody
    public LimsJson selectProjectByRunId(Integer runId){
        LimsJson limsJson = new LimsJson(1,"false");
        if(runId!=null&&runId!=0){
            LimsProjectExample example = new LimsProjectExample();
            LimsProjectExample.Criteria criteria = example.createCriteria();
            criteria.andRunIdEqualTo(runId);
            List<LimsProject> limsProjectList = limsProjectMapper.selectByExample(example);
            if(limsProjectList!=null&&limsProjectList.size()>0&&limsProjectList.get(0)!=null&&limsProjectList.get(0).getDeptId()!=null){
                Department department = departmentMapper.getDeptById(limsProjectList.get(0).getDeptId());
                limsProjectList.get(0).setDeptName(department.getDeptName());
                limsJson.setCode(0);
                limsJson.setMsg("true");
                limsJson.setObject(limsProjectList.get(0));
            }else {
                limsJson.setCode(10);
                limsJson.setMsg("true");
            }
        }else {
            limsJson.setCode(10);
            limsJson.setMsg("true");
        }
        return limsJson;
    }

    /**
    * 创建作者:   刘 欢
    * 创建日期:   16:48 2020/5/28
    * 方法介绍:   判断这个runid下面有没有关联应用
    * 参数说明:   * @param flowFormId
    * 返回值类型：com.xoa.util.lims.LimsJson
    */

    @RequestMapping("findRelationFuncByFlowFromId")
    @ResponseBody
    public LimsJson findRelationFuncByFlowFromId(Integer flowFormId){
        LimsJson limsJson = new LimsJson(1,"false");
        RelationFuncExample example = new RelationFuncExample();
        RelationFuncExample.Criteria criteria = example.createCriteria();
        criteria.andFlowFormIdEqualTo(flowFormId);
        List<RelationFunc> relationFuncList = relationFuncMapper.selectByExample(example);
        if(relationFuncList!=null&&relationFuncList.size()>0&&relationFuncList.get(0)!=null){
            limsJson.setObject(relationFuncList.get(0));
            limsJson.setCode(0);
            limsJson.setMsg("true");
        }else {
            limsJson.setCode(10);
            limsJson.setMsg("true");
        }
        return limsJson;
    }


    /**
     * 查询流程实例
     * @param runId
     * @return
     */
    @RequestMapping("/findWfeFlowRun")
    @ResponseBody
    public LimsJson findWfeFlowRun(HttpServletRequest request, @RequestParam("runId") Integer runId){
        LimsJson limsJson=new LimsJson(1,"err");
        try {
            Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
            limsJson.setObj(findrun(user.getUserId(),runId));
            limsJson.setCount(0);
            limsJson.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            limsJson.setMsg(e.getMessage());
        }
        return limsJson;
    }

    //查询流程实例LimsPAD
    public FlowRun findrun(String userId, Integer runId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("runId", runId);
        FlowRun f = wfeFlowRunMapper.findrun(map);
        if (f != null) {
            String endTime = f.getEndTime();
            if (!StringUtils.checkNull(endTime)) {
                if (endTime.contains(".")) {
                    endTime = endTime.substring(0, endTime.length() - 2);
                    f.setEndTime(endTime);
                }
            }
            if (!StringUtils.checkNull(f.getBeginTime())) {
                f.setBeginTime(DateFormat.getStrTime(DateFormat.getTime(f.getBeginTime())));
            }

            //判断是否要修改
            if (Integer.parseInt(f.getPrcsFlag())>2){
                //修改流程接收时间
                Map map1=new HashMap();
                map1.put("prcsTime", DateFormat.getStrDate(new Date()));
                map1.put("prcsFlag","2");
                map1.put("runId",f.getRunId());
                map1.put("prcsId",f.getPrcsId());
                map1.put("userId",userId);
                map1.put("flowPrcs",f.getFlowPrcs());
                wfeFlowRunPrcsMapper.updateSome(map1);

                //修改上一步状态
                map1.put("prcsFlag", "4");
                map1.put("prcsId", Integer.parseInt(f.getPrcsId()) - 1);
                map1.remove("prcsTime");
                String parent = f.getParent();
                map1.put("flowPrcs", parent);
                map1.remove("userId");
                wfeFlowRunPrcs.updateSql(map1);

            }
        }
        return f;
    }
}
